#include <gtk/gtk.h>

#include "main.h"
#include "win_new.h"
#include "win_about.h"
#include "tree.h"

#include "win_main.h"

GtkWidget *win_main_menubar_widget(GtkWidget * window);

static GtkItemFactoryEntry menu_items[] = {
    {"/_File",          NULL,           NULL,           0, "<Branch>"},
    {"/File/_New sync", "<control>N",   win_new,        0, "<StockItem>",   GTK_STOCK_NEW },
    {"/File/sep1",      NULL,           NULL,           0, "<Separator>" },
    {"/File/_Quit",     "<control>Q",   gtk_main_quit,  0, "<StockItem>",   GTK_STOCK_QUIT},

    {"/_Help",          NULL,           NULL,           0, "<Branch>"},
    {"/Help/About",     NULL,           win_about,      0, "<StockItem>",   GTK_STOCK_ABOUT},
};

static gint nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);

GtkWidget *win_main_widget()
{
    GtkWidget *main_vbox;
    GtkWidget *menubar;

    main_vbox = gtk_vbox_new(FALSE, 1);
    gtk_widget_show(main_vbox);

    menubar = win_main_menubar_widget(globals.win_main);
    gtk_widget_show(menubar);
    globals.tree=tree_new_empty();
    gtk_widget_show(globals.tree->widget);

    gtk_box_pack_start(GTK_BOX(main_vbox), menubar, FALSE, TRUE, 0);
    gtk_box_pack_end(GTK_BOX(main_vbox), globals.tree->widget, TRUE, TRUE, 0);

    return main_vbox;
}

GtkWidget *win_main_menubar_widget(GtkWidget * window)
{
    GtkItemFactory *item_factory;
    GtkAccelGroup *accel_group;

    /* Make an accelerator group (shortcut keys) */
    accel_group = gtk_accel_group_new();

    /* Make an ItemFactory (that makes a menubar) */
    item_factory = gtk_item_factory_new(GTK_TYPE_MENU_BAR, "<main>", accel_group);

    /* This function generates the menu items. Pass the item factory,
       the number of items in the array, the array itself, and any
       callback data for the menu items. */
    gtk_item_factory_create_items(item_factory, nmenu_items, menu_items, NULL);

    /* Attach the new accelerator group to the window. */
    gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);

    /* Finally, return the actual menu bar created by the item factory. */
    return gtk_item_factory_get_widget(item_factory, "<main>");
}
